package com.zjff.myshop.admin.service.impl;

import com.zjff.myshop.entity.SysUser;
import com.zjff.myshop.exception.BusinessException;
import com.zjff.myshop.mapper.SysUserMapper;
import com.zjff.myshop.admin.service.SysUserService;
import com.zjff.myshop.util.MD5Util;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class SysUserServiceImpl implements SysUserService {
    @Resource
    private SysUserMapper sysUserMapper;

    @Override
    public SysUser login(String name, String password) {
        SysUser user = sysUserMapper.selectByName(name);
        if (user == null) {
            throw new BusinessException("用户名不存在或密码错误");
        }

        String passwordMd5 = MD5Util.md5Encode(password + user.getSalt(), "UTF-8");
        if (passwordMd5.equals(user.getPassword())) {
            return user;
        }
        throw new BusinessException("用户名不存在或密码错误");
    }

    @Override
    public Boolean logout() {
        return true;
    }

    @Override
    public SysUser selectByPrimaryKey(Integer id) {
        return sysUserMapper.selectByPrimaryKey(id);
    }
}
